<template>
  <div class="autumn-container">
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/wms/stockDtl/stockDtlList' }">库存明细</el-breadcrumb-item>
      <el-breadcrumb-item>出入库</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <div class="autumn-center">
      <div class="edit-form">
        <el-form ref="editFormRef" size="large" label-width="90px" :model="editForm" :rules="editFormRules">

          <el-row>
            <el-col :span="12">
              <el-form-item label="仓库" prop="warehouseInfoId">
                <el-select v-model="editForm.warehouseInfoId">
                  <el-option v-for="(item, index) in warehouses" :key="index" :label="item.code + '-' + item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="库区" prop="warehouseAreaId">
                <el-select v-model="editForm.warehouseAreaId">
                  <el-option v-for="(item, index) in warehouseAreas" :key="index" :label="item.code + '-' + item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="库位" prop="warehouseLocationId">
                <el-select v-model="editForm.warehouseLocationId">
                  <el-option v-for="(item, index) in warehouseLocations" :key="index" :label="item.code + '-' + item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="商品" prop="productName">
                <el-input readonly v-model="editForm.productName">
                  <template #append>
                    <el-button icon="el-icon-search" @click="showProductDialog"></el-button>
                  </template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="商品批号" prop="batchNo">
                <el-input v-model="editForm.batchNo"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="商品条码" prop="snNo">
                <el-input v-model="editForm.snNo"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="商家批号" prop="customBatchNo">
                <el-input v-model="editForm.customBatchNo"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="数量" prop="stockQty">
                <el-input-number v-model="editForm.stockQty" :min="0.01" :max="99999999.99" :precision="2" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" resize="none" v-model="editForm.remark" />
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>

        <div class="form-foot-button">
          <el-button type="primary" @click="submit(editFormRef)">保存</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </div>
    </div>

    <!-- 商品 -->
    <product-component :title="'上级领导'" :defaultValue="editForm.productId" ref="productRef" @apply="applyProduct" />
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { FormInstance } from 'element-plus'
import ProductComponent from 'src/views/modules/metadata/component/ProductComponent.vue'
import { inStock, outStock } from 'src/api/wms/stock'
import { listWarehouseInfo } from 'src/api/wms/warehouseInfo'
import { listWarehouseArea } from 'src/api/wms/warehouseArea'
import { listWarehouseLocation } from 'src/api/wms/warehouseLocation'

//查询仓库/库区
const warehouses = ref<Array<any>>([])
listWarehouseInfo().then(({ data }) => {
  warehouses.value = data
})

//查询库区
const warehouseAreas = ref<Array<any>>([])
const initWarehouseArea = (sourceId: string) => {
  listWarehouseArea({ warehouseInfoId: sourceId }).then(({ data }) => {
    warehouseAreas.value = data
  })
}

//查询库位
const warehouseLocations = ref<Array<any>>([])
const initWarehouseLocation = (sourceId: string) => {
  listWarehouseLocation({ warehouseAreaId: sourceId }).then(({ data }) => {
    warehouseLocations.value = data
  })
}

// 定义表单数据
const editFormRef = ref<FormInstance>()
const editForm = reactive({
  actionType: '',
  fromWarehouseCategory: '',
  fromWarehouseProperty: '',
  fromWarehouseInfoId: '',
  fromWarehouseInfoCode: '',
  fromWarehouseInfoName: '',
  fromWarehouseAreaId: '',
  fromWarehouseAreaCode: '',
  fromWarehouseAreaName: '',
  fromWarehouseLocationId: '',
  fromWarehouseLocationCode: '',
  fromWarehouseLocationName: '',
  toWarehouseCategory: '',
  toWarehouseProperty: '',
  toWarehouseInfoId: '',
  toWarehouseInfoCode: '',
  toWarehouseInfoName: '',
  toWarehouseAreaId: '',
  toWarehouseAreaCode: '',
  toWarehouseAreaName: '',
  toWarehouseLocationId: '',
  toWarehouseLocationCode: '',
  toWarehouseLocationName: '',
  productId: '',
  productCategory: '',
  productCode: '',
  productName: '',
  productSpec: '',
  productModel: '',
  productUnit: '',
  batchNo: '',
  customBatchNo: '',
  snNo: '',
  stockQty: 1,
  remark: '',
  warehouseInfoId: '',
  warehouseAreaId: '',
  warehouseLocationId: ''
})

const editFormRules = {
  warehouseInfoId: [
    { required: true, message: '请选择仓库', trigger: ['blur', 'change'] }
  ],
  warehouseAreaId: [
    { required: true, message: '请选择库区', trigger: ['blur', 'change'] }
  ],
  warehouseLocationId: [
    { required: true, message: '请选择库位', trigger: ['blur', 'change'] }
  ],
  productName: [
    { required: true, message: '请选择商品', trigger: ['blur', 'change'] }
  ],
  stockQty: [
    { required: true, message: '请输入数量', trigger: ['blur', 'change'] }
  ]
}

// 修改时填充表单数据
const route = useRoute()
if (route.query.actionType) {
  const actionType: string = route.query.actionType as string
  editForm.actionType = actionType
}

//监听仓库
watch(() => editForm.warehouseInfoId, (newVal: string) => {
  if (!newVal) {
    return
  }
  const warehouse = warehouses.value.find((item) => item.id == newVal)
  if (!warehouse) {
    return
  }
  if (editForm.actionType == '1') {
    editForm.toWarehouseInfoId = newVal
    editForm.toWarehouseInfoCode = warehouse.code
    editForm.toWarehouseInfoName = warehouse.name
    editForm.toWarehouseCategory = warehouse.category

  } else if (editForm.actionType == '2') {
    editForm.fromWarehouseInfoId = newVal
    editForm.fromWarehouseInfoCode = warehouse.code
    editForm.fromWarehouseInfoName = warehouse.name
    editForm.fromWarehouseCategory = warehouse.category
  }

  initWarehouseArea(newVal)
})

//监听库区
watch(() => editForm.warehouseAreaId, (newVal: string) => {
  if (!newVal) {
    return
  }
  const warehouseArea = warehouseAreas.value.find((item) => item.id == newVal)
  if (!warehouseArea) {
    return
  }
  if (editForm.actionType == '1') {
    editForm.toWarehouseAreaId = newVal
    editForm.toWarehouseAreaCode = warehouseArea.code
    editForm.toWarehouseAreaName = warehouseArea.name
    editForm.toWarehouseProperty = warehouseArea.property

  } else if (editForm.actionType == '2') {
    editForm.fromWarehouseAreaId = newVal
    editForm.fromWarehouseAreaCode = warehouseArea.code
    editForm.fromWarehouseAreaName = warehouseArea.name
    editForm.fromWarehouseProperty = warehouseArea.property
  }
  initWarehouseLocation(newVal)
})

//监听库位
watch(() => editForm.warehouseLocationId, (newVal: string) => {
  if (!newVal) {
    return
  }
  const warehouseLocation = warehouseLocations.value.find((item) => item.id == newVal)
  if (!warehouseLocation) {
    return
  }
  if (editForm.actionType == '1') {
    editForm.toWarehouseLocationId = newVal
    editForm.toWarehouseLocationCode = warehouseLocation.code
    editForm.toWarehouseLocationName = warehouseLocation.name

  } else if (editForm.actionType == '2') {
    editForm.fromWarehouseLocationId = newVal
    editForm.fromWarehouseLocationCode = warehouseLocation.code
    editForm.fromWarehouseLocationName = warehouseLocation.name
  }
})

// 提交
const submit = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(valid => {
    if (!valid) return
    if (editForm.actionType == '1') {
      inStock(editForm).then(() => {
        cancel()
      })
    } else if (editForm.actionType == '2') {
      outStock(editForm).then(() => {
        cancel()
      })
    }
  })
}

// 取消
const router = useRouter()
const cancel = () => {
  router.push('/wms/stockDtl/stockDtlList')
}

//商品
const productRef = ref()
function showProductDialog() {
  productRef.value.open()
}
const applyProduct = (rows: Array<any>) => {
  const row = rows[0]
  editForm.productId = row.id
  editForm.productCode = row.code
  editForm.productName = row.name
  editForm.productCategory = row.category
  editForm.productModel = row.model
  editForm.productSpec = row.spec
  editForm.productUnit = row.unit
}

</script>

<style lang="scss" scoped></style>

